//
//  Movie.m
//  RGATest
//
//  Created by Pablo Javier Bertola on 2/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Movie.h"
#import "Director.h"


@implementation Movie

+(Movie *)movieWithData:(NSDictionary *)movieData inContext:(NSManagedObjectContext*)context
{
    Movie *movie = nil;
    
    NSFetchRequest* request = [[NSFetchRequest alloc]init];
    request.entity = [NSEntityDescription entityForName:@"Movie" inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"name = %@", [movieData objectForKey:@"Name"]];
    
    NSError *error;
    movie = [[context executeFetchRequest:request error:&error] lastObject];
    
    if (error || !movie) {
        movie = [NSEntityDescription insertNewObjectForEntityForName:@"Movie" inManagedObjectContext:context];
        movie.name = [movieData objectForKey:@"Name"];
        movie.year = [NSNumber numberWithInt:[[movieData objectForKey:@"Year"] intValue]];
        movie.director = [Director directorWithData:[movieData objectForKey:@"Director"] inContext:context];
    }
    NSLog(@"Movie Name: %@", movie.name);
    return movie;
}

@dynamic name;
@dynamic year;
@dynamic director;

    

@end
